package org.bugjail.dataaccess.jdbc.objects;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.bugjail.data.AccessLevel;
import org.bugjail.dataaccess.jdbc.connection.QueryException;
import org.bugjail.dataaccess.jdbc.connection.QueryManager;
import org.bugjail.dataaccess.objects.AccessLevelDAO;

class JDBCAccessLevelDAO implements AccessLevelDAO {
	
	private final String PROPERTIES_FILE = "/org/bugjail/dataaccess/jdbc/properties/accessleveldao.properties";
	
	private final String PROPERTY_INSERT = "insert";
	
	private final String PROPERTY_UPDATE = "update";
	
	private final String PROPERTY_DELETE = "delete";
	
	private final String PROPERTY_RETRIEVE_BY_ID = "retrieve.byid";
	private final String PROPERTY_RETRIEVE_ALL = "retrieve.all";
	private final String PROPERTY_RETRIEVE_BY_NAME = "retrieve.byname";

	@Override
	public void create(AccessLevel element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_INSERT);
				statement.setString(1, element.getName());
				statement.setString(2, element.getDescription());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public void delete(AccessLevel element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_DELETE);
				statement.setInt(1, element.getId());
				statement.executeUpdate();
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}

	@Override
	public List<AccessLevel> retrieveAll() {
		List<AccessLevel> accessLevels = new LinkedList<AccessLevel>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_ALL);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					accessLevels.add(extractAccessLevelsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return accessLevels;
	}

	@Override
	public AccessLevel retrieveById(int id) {
		AccessLevel accessLevel = new AccessLevel();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_ID);
				statement.setInt(1, id);
				ResultSet rs = statement.executeQuery();
				if (rs.next()) {
					accessLevel = extractAccessLevelsFromResultSet(rs, queryManager);
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return accessLevel;
	}

	@Override
	public List<AccessLevel> retrieveByName(String name) {
		List<AccessLevel> accessLevels = new LinkedList<AccessLevel>();
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_RETRIEVE_BY_NAME);
				statement.setString(1, name);
				ResultSet rs = statement.executeQuery();
				while (rs.next()) {
					accessLevels.add(extractAccessLevelsFromResultSet(rs, queryManager));
				}
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			throw new QueryException(e.getMessage());
		}
		return accessLevels;
	}

	@Override
	public void update(AccessLevel element) {
		QueryManager queryManager = null;
		try {
			try {
				queryManager = new QueryManager(PROPERTIES_FILE);
				PreparedStatement statement = queryManager
				.getPreparedStatement(PROPERTY_UPDATE);
				statement.setString(1, element.getName());
				statement.setString(2, element.getDescription());
				statement.setInt(3, element.getId());
				statement.executeUpdate();
				
				QueryManager.commit(queryManager);
			} finally {
				QueryManager.close(queryManager);
			}
		} catch (Exception e) {
			QueryManager.rollback(queryManager);
			throw new QueryException(e.getMessage());
		}
	}
	
	//---------------------------------- PRIVATE METHODS ----------------------------------//
	
	private AccessLevel extractAccessLevelsFromResultSet(ResultSet rs, QueryManager queryManager) throws SQLException {
		AccessLevel accessLevel = new AccessLevel();
		accessLevel.setId(rs.getInt("id"));
		accessLevel.setName(rs.getString("name"));
		accessLevel.setDescription(rs.getString("description"));
		return accessLevel;
	}

}
